<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model
        ref="form"
        :model="model"
        slot="detail"
        :rules="validatorRules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item style="margin-bottom: 0;">
          确定{{isBatch?'批量':''}}发送劝阻短信/智能语音电话？
        </a-form-model-item>
        <a-form-model-item prop="types" style="margin-bottom: 0;">
          <a-checkbox-group v-model="model.types">
            <a-checkbox :value="1">短信</a-checkbox>
            <a-checkbox :value="2">智能语音</a-checkbox>
          </a-checkbox-group>
        </a-form-model-item>
        <a-form-model-item style="margin-bottom: 0;">
          *温馨提示：已过滤掉不满足短信配置要求的号码！
        </a-form-model-item>
        <a-form-model-item v-if="showFlowSubmitButton">
          <a-button @click="submitForm">提 交</a-button>
        </a-form-model-item>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import * as warningApi from '@/api/warning/WarningData.js'
import JFormContainer from '@/components/jeecg/JFormContainer'

export default {
  name: 'BatchDissuadetForm',
  components: {
    JFormContainer
  },
  props: {
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    normal: {
      type: Boolean,
      default: false,
      required: false
    },
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    var validateType = function(rule, value, callback) {
      if (value && value.length===0) {
        callback(new Error('请选择短信类型'))
      } else {
        callback()
      }
    }
    return {
      model: { types: [1, 2] },
      id: '',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        types: [
          { required: false, type: 'array', message: '请选择短信类型', trigger: 'change' },
          { validator: validateType, trigger: 'change' }
        ]
      },
      isBatch: false,
      getSelectKeyDatas: []
    }
  },
  computed: {
    formDisabled() {
      if (this.normal === false) {
        if (this.formData.disabled === false) {
          return false
        } else {
          return true
        }
      }
      return this.disabled
    },
    disabledId() {
      return this.id ? true : false
    },
    showFlowSubmitButton() {
      if (this.normal === false) {
        if (this.formData.disabled === false) {
          return true
        } else {
          return false
        }
      } else {
        return false
      }
    }
  },
  created() {},
  methods: {
    show(record) {
      this.model = record ? Object.assign({}, record) : this.model
      this.id = record ? record.id : ''
      this.visible = true
    },
    submitForm() {
      const type = this.model.types && this.model.types.length > 1 ? 0 : this.model.types[0]
      const parameter = Object.assign({}, { type: type }, this.getSelectKeyDatas)
      this.confirmLoading = true
      this.$refs.form.validate((valid)=> {
        if (valid) {
          warningApi
            .dissuadeMsg(parameter)
            .then(res => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok');
                this.confirmLoading = false
              } else {
                this.$message.warn(res.message)
                this.confirmLoading = false
              }
            })
            .catch(e => {
              this.$message.warn('请求失败！')
              this.confirmLoading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
